package com.runa.monitor.platinterface.netservice.equipment.dtu;

import com.alibaba.fastjson.JSON;
import com.runa.persistence.util.SpringContextUtil;
import com.runa.monitor.platinterface.netservice.equipment.NetEqu;
import com.runa.monitor.platinterface.platService.NetEquService;
import com.runa.rccprotocol.netservice.dtu.DtuResponse;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * 2024/9/2 19:33
 * zhangYB
 */
@Slf4j
@ChannelHandler.Sharable
public class DtuResponseHandler extends SimpleChannelInboundHandler<DtuResponse> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DtuResponse response) {
        log.debug("channelRead0进入，发送消息 ---" + JSON.toJSONString(response));
        NetEqu<String> netEqu;
        NetEquService netEquService = SpringContextUtil.getBean(NetEquService.class);
        if (response.getAddress() == null) {
            netEqu = netEquService.getNetEquBySerialCache(String.valueOf(NetEqu.getSerial(ctx.channel().id().asLongText())));
        } else {
            netEqu = netEquService.getNetEquBySerialCache(response.getAddress());
        }
        if (netEqu != null) {
            netEqu.receiveDtuResponse(response);
        }
    }


}
